package com.data.excel;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Row;

import com.core.update.restTestCaseDetails;
import com.core.update.restTestCaseUpdate;
import com.core.update.restTestResultsUpdate;



public class StartFromhere {
public static void main(String[] args) throws IOException, URISyntaxException{
	if(args==null){
	    
	      FromExcel fromExcel = new FromExcel();
	      File dir1 = new File(".");
	      String filePath = dir1.getCanonicalPath()+"\\TestResults.xls";
	      fromExcel.openExcel(filePath);
	      fromExcel.setWorkbook();
	      String[] userCredentials = fromExcel.getCredentials();
	      String username =  userCredentials[0].trim();
		  String password =  userCredentials[1].trim();
			if(username!="" && password !=""){	
				HSSFSheet sheetResults = fromExcel.getTestResults();
				String testCaseID;
				String build;
				String verdict;
			for(int i=1;i<=fromExcel.getNumberOfTestCases();i++) {
				
				HSSFCell testCaseIDCell = sheetResults.getRow(i).getCell(0,Row.CREATE_NULL_AS_BLANK);
				HSSFCell buildCell = sheetResults.getRow(i).getCell(1,Row.CREATE_NULL_AS_BLANK);
				HSSFCell verdictCell = sheetResults.getRow(i).getCell(2,Row.CREATE_NULL_AS_BLANK);
				if(testCaseIDCell.getCellType()==3){
					if(buildCell.getCellType()==3){
						if(verdictCell.getCellType()==3){
						System.out.println("Reached end of sheet");
						break;
						}System.out.println("There is no Test Case ID and Build, but has Verdict in row: "+i);
						continue;
					}else{
						System.out.println("There is no Test Case ID in row: "+i);
						continue;
					}
					
					
				}else{
					 testCaseID = sheetResults.getRow(i).getCell(0).toString();
				}
				if(buildCell.getCellType()!=3){
					 build = sheetResults.getRow(i).getCell(1).toString();
				}else{
					System.out.println("There is no Build value for Test Case: "+testCaseID);
					continue;
				}
				if(verdictCell.getCellType()!=3){
					verdict = sheetResults.getRow(i).getCell(2).toString();
				}else{
					System.out.println("There is no Verdict for Test Case: "+testCaseID);
					continue;
				}
				 HSSFCell testSetCell = sheetResults.getRow(i).getCell(3, Row.CREATE_NULL_AS_BLANK);
				String testSet;
				if(testSetCell.getCellType()==3){
					testSet = "";
				}else{
					testSet = sheetResults.getRow(i).getCell(3, Row.CREATE_NULL_AS_BLANK).toString();
				}
				
				HSSFCell notesCell = sheetResults.getRow(i).getCell(4, Row.CREATE_NULL_AS_BLANK);
				String notes;
				if(notesCell.getCellType()==3){
					notes = "";
				}else{
					notes = sheetResults.getRow(i).getCell(4, Row.CREATE_NULL_AS_BLANK).toString();
				}
				System.out.println("\nStarted updating test case ID: "+testCaseID+"\n");
				System.out.println("\n This is "+i+" test case out of "+ (fromExcel.getNumberOfTestCases()-1)+" Test cases\n\n" );
				
				//restTestResultsUpdate.updateRally(username, password, testCaseID.trim(),verdict.trim(),notes.trim(), build.trim(), testSet.trim());
				restTestResultsUpdate.updateRally(username, password, testCaseID.trim(),verdict.trim(),notes.trim(), build.trim(), testSet.trim());
			}
			System.out.println("Done");
			
		}else{
			if (username==null){
			System.out.println("User name cell in Sheet 1 is null. Please update the cell");
			}else if(password == null){
				System.out.println("Password cell in Sheet 1 is null. Please update the cell");
			}
		}
}else if(args[0].equals("testcasedetails")){
	FromExcel fromExcel = new FromExcel();
    File dir1 = new File(".");
    String filePath = dir1.getCanonicalPath()+"\\TestResults.xls";
    fromExcel.openExcel(filePath);
    fromExcel.setWorkbook();
    String[] userCredentials = fromExcel.getCredentials();
    String username =  userCredentials[0].trim();
	  String password =  userCredentials[1].trim();
		if(username!="" && password !=""){	
			HSSFSheet sheetResults = fromExcel.getTestResults();
			String testCaseID;
			String build;
			String verdict;
		for(int i=1;i<=fromExcel.getNumberOfTestCases();i++) {
			
			HSSFCell testCaseIDCell = sheetResults.getRow(i).getCell(0,Row.CREATE_NULL_AS_BLANK);
		/*	HSSFCell buildCell = sheetResults.getRow(i).getCell(1,Row.CREATE_NULL_AS_BLANK);
			HSSFCell verdictCell = sheetResults.getRow(i).getCell(2,Row.CREATE_NULL_AS_BLANK);
			if(testCaseIDCell.getCellType()==3){
				if(buildCell.getCellType()==3){
					if(verdictCell.getCellType()==3){
					System.out.println("Reached end of sheet");
					break;
					}System.out.println("There is no Test Case ID and Build, but has Verdict in row: "+i);
					continue;
				}else{
					System.out.println("There is no Test Case ID in row: "+i);
					continue;
				}
				
				
			}else{*/
				 testCaseID = sheetResults.getRow(i).getCell(0).toString();
			/*}
			if(buildCell.getCellType()!=3){
				 build = sheetResults.getRow(i).getCell(1).toString();
			}else{
				System.out.println("There is no Build value for Test Case: "+testCaseID);
				continue;
			}
			if(verdictCell.getCellType()!=3){
				verdict = sheetResults.getRow(i).getCell(2).toString();
			}else{
				System.out.println("There is no Verdict for Test Case: "+testCaseID);
				continue;
			}
			 HSSFCell testSetCell = sheetResults.getRow(i).getCell(3, Row.CREATE_NULL_AS_BLANK);
			String testSet;
			if(testSetCell.getCellType()==3){
				testSet = "";
			}else{
				testSet = sheetResults.getRow(i).getCell(3, Row.CREATE_NULL_AS_BLANK).toString();
			}
			
			HSSFCell notesCell = sheetResults.getRow(i).getCell(4, Row.CREATE_NULL_AS_BLANK);
			String notes;
			if(notesCell.getCellType()==3){
				notes = "";
			}else{
				notes = sheetResults.getRow(i).getCell(4, Row.CREATE_NULL_AS_BLANK).toString();
			}
			System.out.println("\nStarted updating test case ID: "+testCaseID+"\n");
			System.out.println("\n This is "+i+" test case out of "+ (fromExcel.getNumberOfTestCases()-1)+" Test cases\n\n" );
			*/
			//restTestResultsUpdate.updateRally(username, password, testCaseID.trim(),verdict.trim(),notes.trim(), build.trim(), testSet.trim());
			String testCaseDetailsMap = restTestCaseDetails.getTestCaseDetails(username, password, testCaseID);
			sheetResults.getRow(i).getCell(1, Row.CREATE_NULL_AS_BLANK).setCellValue(testCaseDetailsMap);
			fromExcel.saveWorkBook(filePath);
		}
		System.out.println("Done");
		
	}else{
		if (username==null){
		System.out.println("User name cell in Sheet 1 is null. Please update the cell");
		}else if(password == null){
			System.out.println("Password cell in Sheet 1 is null. Please update the cell");
		}
	}
}

}		
	   



}

